package models

import (
	"os"
	"path"
	"time"

	"github.com/Unknwon/com"
	"github.com/astaxie/beego/orm"
	//下划线只执行初始化函数，而不其他函数调用
	_ "github.com/mattn/go-sqlite3"
)

const (
	_DB_NAME        = "data/beeblog.db"
	_SQLITE3_DRIVER = "sqlite3"
)

//orm:index；建表时帮忙建个索引
type Category struct {
	Id              int64     //如果名称为Id,类型为int64或者32，orm自动认为是主键
	Title           string    //orm默认设置字节为255
	Created         time.Time `orm:"index"` //创建时间
	Views           int64     `orm:"index"` //浏览次数
	TopicTime       time.Time `orm:"index"`
	TopicCount      int64     //统计文章
	TopicLastUserId int64     //最后操作人ID
}

//文章
type Topic struct {
	Id               int64
	Uid              int64     //创作人
	Title            string    //标题
	Content          string    `orm:"size(5000)"` //内容
	Attachment       string    //附件
	Created          time.Time `orm:"index"` //创建时间
	Updated          time.Time `orm:"index"` //更新时间
	Views            int64     `orm:"index"` //浏览次数
	Author           string    //作者
	ReplyTime        time.Time //回复时间
	ReplyCount       int64     //评论个数
	RepleyLastUserId int64     `orm:"index"` //最后的评论用户ID
}

func RegisterDB() {
	//是否存在
	if !com.IsExist(_DB_NAME) {
		os.MkdirAll(path.Dir(_DB_NAME), os.ModePerm)
		os.Create(_DB_NAME)
	}

	orm.RegisterModel(new(Category), new(Topic))
	orm.RegisterDriver(_SQLITE3_DRIVER, orm.DRSqlite)

	//强制只能用default,驱动名称，数据库名称，最大连接数
	orm.RegisterDataBase("default", _SQLITE3_DRIVER, _DB_NAME, 10)

}
